package com.wolf.etl.core.rule;

import com.wolf.etl.core.ETLManager;
import com.wolf.etl.core.WriteBackManager;
import com.wolf.etl.core.rule.convert.AbstractConvertRule;
import com.wolf.etl.enumeration.APPLICABLE_SCOPE;
import com.wolf.etl.enumeration.CONVERT_RULE_TYPE;
import com.wolf.etl.model.EtlTransformRuleModel;
import com.wolf.etl.model.EtlTransformRuleTypeModel;
import com.wolf.etl.vo.BuildSqlParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author sdyang
 * @date 2020/2/28 10:13
 **/
public class TransformRuleManager {

    private static Logger logger = LoggerFactory.getLogger(WriteBackManager.class);

    private static final String logger_prefix = "wolf-etl#com.wolf.etl.core.rule#TransformRuleManager";

    private BuildSqlParam param;

    private ETLManager manager;

    public static TransformRuleManager getInstance(ETLManager manager, BuildSqlParam param) {

        TransformRuleManager transformRuleManager = new TransformRuleManager();
        transformRuleManager.manager = manager;
        transformRuleManager.param = param;
        return transformRuleManager;

    }

    public String execSource(String value, Long transformDefineId) {

        String result = value;

        List<EtlTransformRuleModel> rules = param.getConvertRuleMap().get(transformDefineId);
        if(CollectionUtils.isEmpty(rules)){
            return result;
        }

        for (EtlTransformRuleModel rule : rules) {

            EtlTransformRuleTypeModel ruleType = param.getRuleTypeMap().get(rule.getType_id());

            if (ruleType.getApplicable_scope().equalsIgnoreCase(APPLICABLE_SCOPE.Target.toString())) {
                rules.remove(rule);
            }

        }
        result = renderByRules(value, rules);
        return result;
    }

    public String execTarget(String value, Long transRuleId) {

        String result = value;

        List<EtlTransformRuleModel> rules = param.getCheckRuleMap().get(transRuleId);
        for (EtlTransformRuleModel rule : rules) {

            EtlTransformRuleTypeModel ruleType = param.getRuleTypeMap().get(rule.getType_id());

            if (ruleType.getApplicable_scope().equalsIgnoreCase(APPLICABLE_SCOPE.Source.toString())) {
                rules.remove(rule);
            }
        }

        result = renderByRules(value, rules);
        return result;
    }


    public String renderByRules(String cloumn, List<EtlTransformRuleModel> rules) {

        String result = cloumn;

        if (CollectionUtils.isEmpty(rules)) {
            return result;
        }

        for (EtlTransformRuleModel rule : rules) {

            String alias = param.getAliasMap().get(rule.getTrans_def_id());
            String param1 = getRenderValue(alias, rule.getParam1(), result);
            String param2 = getRenderValue(alias, rule.getParam2(), result);
            String param3 = getRenderValue(alias, rule.getParam3(), result);
            String param4 = getRenderValue(alias, rule.getParam4(), result);

            EtlTransformRuleTypeModel typeModel = param.getRuleTypeMap().get(rule.getType_id());

            AbstractConvertRule convertRule = AbstractConvertRule.getInstance(CONVERT_RULE_TYPE.valueOf(typeModel.getCode()), manager);
            AbstractConvertRule.RetInfo retInfo = convertRule.convert(rule.getTransform_cxt(), cloumn, param1,param2,param3,param4);
            result = retInfo.getTargetInfo();

        }
        return result;
    }

    private String getRenderValue(String alias, String cloumn, String result) {
        if (StringUtils.isEmpty(cloumn)) {
            return "";
        }

        if (cloumn.contains("[RULE_ID]")) {
            return result;
        }
        if (cloumn.contains("COLUMN")) {
            String columnName = cloumn.substring(cloumn.indexOf("[COLUMN]${") + 10, cloumn.length() - 1);
            return String.format("%s.%s", alias, columnName);
        }
        return cloumn;
    }
}
